// pages/mine/addPet/index.js
import {formatDate} from '../../../../utils/util'
const app=getApp()
Page({
  data:{
    arrayLength:'',
    tempFormData:{
      pickerPetType:'狗',
      birthdayData: formatDate(new Date()),
      petPhoto:'',
      age:'',
    },
    petType:'',
    timeNow: formatDate(new Date()),
  },
  onLoad(options){
    var that = this
    let arrayLength = parseInt(options.arrayLength)
    // petType
    wx.request({
      url: app.globalData.prefixUrl + '/chong/pet/type',
      method:"GET",
      success(e){
          console.log("获取所有宠物类型数据成功: " , e)
          that.setData({
            petType: e.data,
          })
      },
      fail(e){
        console.log("获取所有宠物类型数据失败: " + e)
      }
    })
  },

  changePetImg(){
    var that = this 
    wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都
        sourceType: ['album', 'camera'], //从相册选择
        success: (res) => {
          console.log(res)
          let imgPath = res.tempFilePaths[0]
          // 上传图片
          wx.uploadFile({
            filePath: imgPath,
            name: 'file',
            url: app.globalData.prefixUrl + '/chong/wx/upload?userId=' + app.globalData.userId,
            header: {
              "Content-Type": "multipart/form-data"
            },
            success(e) {
              console.log("\n图片上传成功 "+e.data)
              that.setData({
                'tempFormData.petPhoto':e.data
              })
              // that.data.pet.petPhoto = e.data
              console.log("图片地址：" + that.data.tempFormData.petPhoto)
            },
            fail(e){
                console.log("\n图片上传失败: ", e)
            }
          })
          }
        }
      );
},

  changeDate(e){
    var that = this
    console.log("当前选中生日时间: ", e.detail.value)
    that.setData({ 
      'tempFormData.birthdayData':e.detail.value,
  });
    that.getAgeByBirthdar()
  },

  bindPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      pickerIndex: e.detail.value,
    })
    that.setData({
      'tempFormData.pickerPetType':that.data.petType[this.data.pickerIndex].type,
    })
    console.log("添加的宠物类型: ", that.data.tempFormData.pickerPetType)
  },

  getAgeByBirthdar(){
    var that = this
    let birthday = new Date(that.data.tempFormData.birthdayData.replace(/-/g, "\/"))
    let now = new Date
    let age = now.getFullYear()-birthday.getFullYear()-((now.getMonth()<birthday.getMonth()|| now.getMonth()==birthday.getMonth() && now.getDate()<birthday.getDate())?1:0);
    that.setData({
      'tempFormData.age': age
    })
    console.log("当前日期: ", now)
    console.log("宠物当前年龄: ", that.data.tempFormData.age)
  },
  
  petFormSubmit:function(e){
    var that = this
    var formData=e.detail.value;
    //获取上一个页面的对象
    var pages=getCurrentPages()
    var prevPage=pages[pages.length-2]
    //调用上一个页面的setData()方法，把数据存储到上一个页面中去
    let tempPet = {}
    tempPet.nickname = formData.nickname,
    tempPet.petPhoto = that.data.tempFormData.petPhoto,
    tempPet.type = that.data.tempFormData.pickerPetType,
    tempPet.sex = formData.sex,
    tempPet.age = that.data.tempFormData.age,
  
    // 插入新宠物
    wx.request({
      url: app.globalData.prefixUrl + "/chong/pet/pet" ,
      data:{
        userId:app.globalData.userId,
        nickname:formData.nickname,
        petPhoto:that.data.tempFormData.petPhoto,
        type:that.data.tempFormData.pickerPetType,
        sex:formData.sex,
        age:that.data.tempFormData.age,
        birthday:that.data.tempFormData.birthdayData,
        weight:formData.weight,
        height:formData.height,
        body_length:formData.body_length,
      },
      method:"POST",
      header:{
        'Content-type': "application/x-www-form-urlencoded"
      },
      success(e){
        console.log("插入宠物成功： ", e)
        let petInfos = prevPage.data.petInfos
        petInfos.splice(that.data.arrayLength - 1, 0, tempPet)
        prevPage.setData({
          petInfos
        })
      },
      fail(e){
        console.log("插入宠物失败： ", e)
      }
    })
    //返回上一个页面
    wx.navigateBack()
  }
})